//投入限制经理类
import math from "@/_js/math";
const weexFunction = weex.requireModule('WeexFunction');

export default {

  /** 获取投入限制
   * 
   * @param {String} type ai(ai) define(自定义) many（多币对）
   * @param {String} name 策略名称
   * @param {String} params //参数数组
   * @param {String} investCoin //投资币
   * @param {JSON} pairMap //交易对
   * @param {String} pairNum //币对数量
   * @param {JSON} limitMap //投入限制
   */
  getInvestLimit(type, name, params, investCoin, pairMap, limitMap, pairNum) {


    var min = limitMap ? math.xiaoShuDelete0(limitMap.minInvest) : '100';
    var max = limitMap ? math.xiaoShuDelete0(limitMap.maxInvest) : '50000';
    var limit;
    if (type == 'ai') {
      limit = this.getLimit(min, max, investCoin, pairMap);
    } 
    
    else if (type == 'define') {
      switch (name) {
        case '网格交易':
          limit = this.getLimitGrid(min, max, params, investCoin, pairMap);
          break;

        case '海风马丁':
          limit = this.getLimitHf(min, max, params, investCoin, pairMap);
          break;

        case '马丁追踪':
          limit = this.getLimitMd(min, max, params, investCoin, pairMap);
          break;
          
        case '海浪网格':
          limit = this.getLimitSea(min, max, params, investCoin, pairMap);
          break;

        case '恒量定投':
          limit = this.getLimitWeigh(min, max, params, investCoin, pairMap);
          break;
      
        default:
          limit = this.getLimit(min, max, investCoin, pairMap);
          break;
      }
    }
    
    else if (type == 'many') {

      switch (name) {
        case '网格交易':
          limit = this.getLimitGridMany(min, max, params, investCoin, pairMap, pairNum);
          break;

        case '海风马丁':
          limit = this.getLimitHfMany(min, max, params, investCoin, pairMap, pairNum);
          break;

        case '马丁追踪':
          limit = this.getLimitMdMany(min, max, params, investCoin, pairMap, pairNum);
          break;
          
        case '海浪网格':
          limit = this.getLimitSeaMany(min, max, params, investCoin, pairMap, pairNum);
          break;

        case '恒量定投':
          limit = this.getLimitWeighMany(min, max, params, investCoin, pairMap, pairNum);
          break;
      
        default:
          limit = this.getLimit(min, max, investCoin, pairMap);
          break;
      }
    }

    return limit;
  },
  
  /** 获取基础限制-价格折合
   * 
   * @param {String} min 最小投入
   * @param {String} max 最大投入
   * @param {String} investCoin 投资币
   * @param {JSON} pairMap 交易对
   */
  getLimit(min, max, investCoin, pairMap){
    
    var xiaoshu = this.getXiaoshu(investCoin, pairMap);
    if (investCoin != 'USDT') {
      var price = (investCoin == pairMap.baseCoin) ? pairMap.baseCoinUsdPrice : pairMap.usdPrice;
      min = math.chu(min, price, xiaoshu, 2);
      max = math.chu(max, price, xiaoshu, 2);
    }
    return {'min':min, 'max':max, 'propose':'', 'xiaoshu':xiaoshu}
  },

  /******************************************* 自定义*/
  /** “网格”
   * 
   * 范围：最小投入，大于“单格投入”
   * 建议：最大挂单数 * 单格投入 
   */
  getLimitGrid(min, max, params, investCoin, pairMap){

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围
    var on = this.getParamValue(params, '单格投入');
    if (on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    min = this.getMin(min, max, on);


    //建议投入
    var order = this.getParamValue(params, '最大挂单数');
    if (order == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    var propose = math.cheng(order, on, xiaoshu, 2);
    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** 海风马丁
   * 
   * 范围：最小投入，大于“首单投入”
   * 建议：
   *     倍数补仓（补仓金额模式0）：
   *        1、补仓金额倍数 ≠ 1：【（补仓金额倍数^最大做单数-1）/（补仓金额倍数-1）】*首单投入
   *        2、补仓金额倍数 ＝ 1：最大做单数*首单投入
   *     增量补仓（补仓金额模式1）：【最大做单数 * 首单投入 +（最大做单数^2-最大做单数）* 补仓金额增量/2】
   */
  getLimitHf(min, max, params, investCoin, pairMap) {

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围
    var on = this.getParamValue(params, '首单投入');
    if (on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    min = this.getMin(min, max, on);


    //建议投入
    var type = this.getParamValue(params, '补仓金额模式');
    var order = this.getParamValue(params, '最大做单数');
    if (type == undefined || order == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    if(type == 0) {
      var multiple = this.getParamValue(params, '补仓金额倍数');
      if (multiple == undefined) {
        return {'min':min, 'max':max, 'propose':''};
      }
      var propose;
      if (multiple == 1) {
        propose = math.cheng(order, on, xiaoshu, 2);
      } else {
        
        var _multiple = math.jian(multiple, '1');
        propose = Math.pow(Number(multiple), Number(order)) - 1;
        propose = math.chu(propose, _multiple);
        propose = math.cheng(propose, on, xiaoshu, 2);
      }
    } else {

      var bulking = this.getParamValue(params, '补仓金额增量');
      if (bulking == undefined) {
        return {'min':min, 'max':max, 'propose':''};
      }
      var _propose = math.cheng(order, on);
      propose = Math.pow(Number(order), 2) - order;
      propose = math.cheng(propose, bulking);
      propose = math.chu(propose, '2');
      propose = math.jia(_propose, propose, xiaoshu, 2);
    }

    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** “马丁”
   * 
   * 范围：最小投入，大于“首单金额”
   * 建议：((金额追投倍数^最大做单数) - 1) * 首单金额
   */
  getLimitMd(min, max, params, investCoin, pairMap){

    
    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围
    var on = this.getParamValue(params, '首单金额');
    if (on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    min = this.getMin(min, max, on);

    //建议投入
    var multiple = this.getParamValue(params, '金额追投倍数');
    var order = this.getParamValue(params, '最大做单数');
    if (multiple == undefined || order == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    var propose = Math.pow(Number(multiple), Number(order)) - 1;
    propose = math.cheng(propose, on, xiaoshu, 2);
    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** “海浪”
   * 
   * 范围：最小投入，大于“首单投入”
   * 建议：最大挂单数 * 首单投入 * 2
   */
  getLimitSea(min, max, params, investCoin, pairMap){

    
    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围
    var on = this.getParamValue(params, '首单投入');
    if (on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    min = this.getMin(min, max, on);

    //建议投入
    var order = this.getParamValue(params, '最大挂单数');
    if (order == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    var propose = math.cheng(order, on);
    var propose = math.cheng(propose, '2', xiaoshu, 2);
    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** 衡量定投
   * 
   * 范围：最小投入，大于“单笔金额”
   * 建议：无
   */
  getLimitWeigh(min, max, params, investCoin, pairMap){

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;

    //投入范围
    var on = this.getParamValue(params, '单笔金额');
    if (on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    min = this.getMin(min, max, on);
    return {'min':min, 'max':max, 'propose':''};
  },

  /******************************************* 多币对*/
  /** “网格”
   * 
   * 范围：最小投入，大于“最小投入(服务器返回) * 币对数量”
   * 建议：最大挂单数 * 币对数量 * 单格投入
   */
  getLimitGridMany(min, max, params, investCoin, pairMap, pairNum){

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;
    
    //投入范围---通过这个方法获取折合
    var minx = this.getLimit('1000', '1000', investCoin, pairMap);
    minx = math.cheng(minx.min, pairNum, xiaoshu, 2);
    min = this.getMin(min, max, minx);

    //建议投入
    var on = this.getParamValue(params, '单格投入');
    var order = this.getParamValue(params, '最大挂单数');
    if (order == undefined || on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    var propose = math.cheng(order, pairNum);
    propose = math.cheng(propose, on, xiaoshu, 2);
    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** 海风马丁
   * 
   * 范围：最小投入，大于“最小投入(服务器返回) * 币对数量”
   * 建议：
   *     倍数补仓（补仓金额模式0）：
   *        1、补仓金额倍数 ≠ 1：【（补仓金额倍数^最大做单数-1）/（补仓金额倍数-1）】*首单投入*币对数量
   *        2、补仓金额倍数 ＝ 1：最大做单数*首单投入*币对数量
   * 
   *     增量补仓（补仓金额模式1）：最大做单数 * 首单投入 +（最大做单数^2-最大做单数）* 补仓金额增量/2】*币对数量
   */
  getLimitHfMany(min, max, params, investCoin, pairMap, pairNum) {

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围---通过这个方法获取折合
    var minx = this.getLimit('1000', '1000', investCoin, pairMap);
    minx = math.cheng(minx.min, pairNum, xiaoshu, 2);
    min = this.getMin(min, max, minx);

    //建议投入
    var on = this.getParamValue(params, '首单投入');
    var type = this.getParamValue(params, '补仓金额模式');
    var order = this.getParamValue(params, '最大做单数');
    if (type == undefined || order == undefined || on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    if(type == 0) {
      var multiple = this.getParamValue(params, '补仓金额倍数');
      if (multiple == undefined) {
        return {'min':min, 'max':max, 'propose':''};
      }
      var propose;
      if (multiple == 1) {
        propose = math.cheng(order, on);
        propose = math.cheng(propose, pairNum, xiaoshu, 2);
      } else {
        
        var _multiple = math.jian(multiple, '1');
        propose = Math.pow(Number(multiple), Number(order)) - 1;
        propose = math.chu(propose, _multiple);
        propose = math.cheng(propose, on);
        propose = math.cheng(propose, pairNum, xiaoshu, 2);
      }
    } else {

      var bulking = this.getParamValue(params, '补仓金额增量');
      if (bulking == undefined) {
        return {'min':min, 'max':max, 'propose':''};
      }
      var _propose = math.cheng(order, on);
      propose = Math.pow(Number(order), 2) - order;
      propose = math.cheng(propose, bulking);
      propose = math.chu(propose, '2');
      propose = math.jia(_propose, propose);
      propose = math.cheng(propose, pairNum, xiaoshu, 2);
    }

    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** “马丁”
   * 
   * 范围：最小投入，大于“最小投入(服务器返回) * 币对数量”
   * 建议：币对数量 *（(金额追投倍数^最大做单数)-1）* 首单金额
   */
  getLimitMdMany(min, max, params, investCoin, pairMap, pairNum){

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围---通过这个方法获取折合
    var minx = this.getLimit('1000', '1000', investCoin, pairMap);
    minx = math.cheng(minx.min, pairNum, xiaoshu, 2);
    min = this.getMin(min, max, minx);

    //建议投入
    var order = this.getParamValue(params, '最大做单数');
    var on = this.getParamValue(params, '首单金额');
    var multiple = this.getParamValue(params, '金额追投倍数');
    if (multiple == undefined || order == undefined || on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    var propose = Math.pow(Number(multiple), Number(order)) - 1;
    propose = math.cheng(propose, pairNum);
    propose = math.cheng(propose, on, xiaoshu, 2);
    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },

  /** “海浪”
   * 
   * 范围：最小投入，大于“最小投入(服务器返回) * 币对数量”
   * 建议：最大做单数 * 首单投入 * 币对数量 * 2
   */
  getLimitSeaMany(min, max, params, investCoin, pairMap, pairNum){

    
    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围---通过这个方法获取折合
    var minx = this.getLimit('1000', '1000', investCoin, pairMap);
    minx = math.cheng(minx.min, pairNum, xiaoshu, 2);
    min = this.getMin(min, max, minx);

    //建议投入
    var order = this.getParamValue(params, '最大做单数');
    var on = this.getParamValue(params, '首单投入');
    if (order == undefined || on == undefined) {
      return {'min':min, 'max':max, 'propose':''};
    }
    var propose = math.cheng(order, on);
    propose = math.cheng(propose, pairNum);
    var propose = math.cheng(propose, '2', xiaoshu, 2);
    propose = this.getPropose(min, max, propose);
    return {'min':min, 'max':max, 'propose':propose};
  },
  
  /** 衡量定投
   * 
   * 范围：最小投入，大于“最小投入(服务器返回) * 币对数量”
   * 建议：无
   */
  getLimitWeighMany(min, max, params, investCoin, pairMap, pairNum){

    //基础限制
    var limit = this.getLimit(min, max, investCoin, pairMap);
    min = limit.min;
    max = limit.max;
    var xiaoshu = limit.xiaoshu;

    //投入范围---通过这个方法获取折合
    var minx = this.getLimit('1000', '1000', investCoin, pairMap);
    minx = math.cheng(minx.min, pairNum, xiaoshu, 2);
    min = this.getMin(min, max, minx);
    return {'min':min, 'max':max, 'propose':''};
  },

  /******************************************* 辅助方法*/
  //获取小数
  getXiaoshu(investCoin, pairMap) {

    var bol = investCoin == pairMap.baseCoin;
    var xiaoshu = bol ? pairMap.baseCoinDecimal : pairMap.counterCoinDecimal;
    xiaoshu = math.getNumber(xiaoshu);
    return xiaoshu;
  },

  //获取指定参数值
  getParamValue(params, name){

    for (let index = 0; index < params.length; index++) {
      var param = params[index];
      if (param.name == name) {
        return( param.value + '');
      }
    }
    return undefined;
  },

  // 获取最小
  getMin(min, max, on) {
    min = Number(min) > Number(on) ? min : on;
    min = Number(max) > Number(min) ? min : max;
    return min;
  },

  // 获取推荐
  getPropose(min, max, propose) {

    if (Number(propose) < Number(min)) {
      return min;
    } else if (Number(propose) > Number(max)) {
      return max;
    } else {
      return propose;
    }
  }
}